package cn.stylefeng.guns.modular.util.itext;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;

/**
 * @Description: TODO
 * @Author jzy
 * @Date 2019/12/11 0011
 **/
public class ImageBackgroundEvent implements PdfPCellEvent {

    protected Image image;

    public ImageBackgroundEvent(Image image) {
        this.image = image;
    }
    @Override
    public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
        try {
            PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
            //背景图片位置public Rectangle(float llx, float lly, float urx, float ury);
            //llx 为Left ，lly 为Bottom,urx 为Right，ury 为Top
            //其中：Width=Right - Left   Heigth = Top - Bototom
            //坐标原点为：左下角
            position.setLeft(position.getLeft() + 56);
            position.setRight(position.getRight() - 56);
            position.setTop(position.getTop() - 15);
            position.setBottom(position.getBottom() + 15);
            image.scaleAbsolute(position);
            //image.setAbsolutePosition(position.getLeft(), position.getBottom());
            image.setAbsolutePosition(position.getLeft(), position.getBottom());
            cb.addImage(image);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}
